<template>
  <div style="display: inline-block">
    <el-dialog :visible.sync="dialog" :title="title" append-to-body width="500px" @close="cancel">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
        <el-form-item label="当前公司" prop="cpyCode">
          <el-select v-model="form.cpyCode" style="width: 300px;" placeholder="请选择">
            <el-option
              v-for="(item, index) in cpyList"
              :key="item.cpyName + index"
              :label="item.cpyName"
              :value="item.cpyCode"/>
          </el-select>
        </el-form-item>
        <el-form-item label="默认权限机构" prop="orgCode">
          <treeselect
            v-model="form.orgCode"
            :options="orgSets"
            :searchable="true"
            :clearable="true"
            :show-count="true"
            :flat="true"
            :is-default-expanded="true"
            :normalizer="orgNormalizer"
            style="width: 300px;"
            placeholder="选择默认机构"
            @select="orgSetSelect" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCpyByUserAccount, getPrivOrgSetByUserAccount, updateCpyOrgSet } from '@/api/user'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  components: { Treeselect },
  data() {
    return {
      loading: false, dialog: false, title: '修改默认机构',
      orgSets: [],
      cpyList: [],
      form: {
        cpyCode: '',
        orgCode: ''
      },
      rules: {
        cpyCode: [
          { required: true, message: '请选择公司', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    getCpyByUserAccount() {
      getCpyByUserAccount().then(res => {
        this.cpyList = res.data
      })
    },
    getPrivOrgSetByUserAccount(cpyCode) {
      getPrivOrgSetByUserAccount(cpyCode).then(res => {
        this.orgSets = res.data
      })
    },
    orgNormalizer(node) {
      return {
        id: node.orgCode,
        label: node.orgName
      }
    },
    orgSetSelect(node, instanceId) {
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          updateCpyOrgSet(this.form).then(res => {
            this.resetForm()
            this.$notify({
              title: '修改成功，请重新登录',
              type: 'success',
              duration: 1500
            })
            this.loading = false
            this.$confirm('确定注销并退出系统吗？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$store.dispatch('LogOut', '').then(() => {
                location.reload() // 为了重新实例化vue-router对象 避免bug
              })
            })
          }).catch(err => {
            this.loading = false
            console.log(err)
          })
        } else {
          return false
        }
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
